package cn.bdqn.service.course;

import cn.bdqn.dao.course.CourseMapper;
import cn.bdqn.pojo.course.Course;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseService {
    @Autowired
    private CourseMapper courseMapper;
    //查询所有课程信息
    public List<Course> selectAllCourse(){
        return courseMapper.selectList(null);
    }

    //获取课程总数
    public Long selectCourseCount(){return courseMapper.selectCount(null);}
    //根据状态获取课程数量
    public Long selectCourseCountByStatus(Integer status){return courseMapper.selectCount(new QueryWrapper<Course>().eq("course_status",status));}
    //根据课程名称查询课程集合
    public List<Course> selectCourseByNameAndTagAndStatusAndPage(String courseName,Integer[] tagList,Integer courseStatus){
        return courseMapper.selectCourseByNameAndTagAndStatusAndPage(courseName,tagList,courseStatus);
    }
    //根据课程标签查询课程信息
    public List<Course> selectCourseByTagId(Integer[] tagIds){
        return courseMapper.selectCourseByTagId(tagIds);
    }
    //根据状态获取课程信息
    public List<Course> selectCourseIdAndName(){
        return courseMapper.selectCourseIdAndName();
    }
    //根据课程id查询课程信息
    public Course selectCourseById(Integer courseId){return courseMapper.selectById(courseId);}

    //根据id修改课程状态管理
    public Integer updateCourseStatusById(Integer id,Integer status){
        return  courseMapper.update(null,new UpdateWrapper<Course>().eq("course_id",id).set("course_status",status));
    }

    //根据课程id修该课程信息
    public Integer updateCourseById(Course course){
        return courseMapper.updateCourseById(course);
    }
    //添加课程信息
    public Integer insertCourse(Course course){
        return courseMapper.insert(course);
    }
    //根据课程id删除课程信息
    public Integer deleteCourseById(Integer id){
        return courseMapper.delete(new QueryWrapper<Course>().eq("course_id",id));
    }

    public List<Integer> requestAll(){
        List<Integer> list=new ArrayList<>();
        //获取已上架课程
        Long aLong = selectCourseCountByStatus(1);
        //获取一下架课程
        Long aLong1 = selectCourseCountByStatus(0);
        list.add(Math.toIntExact(aLong));
        list.add(Math.toIntExact(aLong1));
        return list;
    }

}
